import 'package:flutter/material.dart';
import 'package:flutter_base_framework/base_framework_export.dart';

///封装缩放动画

class ScaleAnimWidget extends BaseWidget {
  final Widget child; // 包裹的子组件
  final double scaleBegin; // 起始缩放比例
  final double scaleEnd; // 结束缩放比例
  final int duration; // 单次动画时长
  final Curve curve; // 动画曲线
  const ScaleAnimWidget({
    super.key,
    required this.child,
    this.scaleBegin = 1.0,
    this.scaleEnd = 1.5,
    this.duration = 1000,
    this.curve = Curves.easeInOut,
  });

  @override
  State<ScaleAnimWidget> createState() => _ScaleAnimWidgetState();
}

class _ScaleAnimWidgetState extends BaseWidgetState<ScaleAnimWidget> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _animation;

  @override
  void initState() {
    super.initState();

    // 初始化动画控制器
    _controller = AnimationController(
      duration: Duration(milliseconds: widget.duration),
      vsync: this,
    )..repeat(reverse: true); // 循环播放

    // 创建缩放动画
    _animation = Tween<double>(
      begin: widget.scaleBegin,
      end: widget.scaleEnd,
    ).animate(
      CurvedAnimation(
        parent: _controller,
        curve: widget.curve,
      ),
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _animation,
      builder: (context, child) {
        return Transform.scale(
          scale: _animation.value,
          child: child,
        );
      },
      child: widget.child,
    );
  }
}
